#include <iostream>       // std::cout, std::endl
#include <thread>         // std::this_thread::sleep_for
#include <chrono> 
using namespace std;

void func1() {
    std::this_thread::sleep_for(std::chrono::seconds(5));
}
void func2() {
    cout << this_thread::get_id() << endl;
	thread t([] {cout << this_thread::get_id() << endl; });
	t.detach();
}
void thread_func1(int a) {
    for (int i = 0; i < a; i++) {
        cout << "sub thread is running!" << endl;
        std::this_thread::sleep_for(std::chrono::seconds(1));
    }
}
void func3() {
    thread task(thread_func1, 5);
    task.detach();
    for (int i = 0; i < 3; i++) {
        cout << "main thread is running!" << endl;
        std::this_thread::sleep_for(std::chrono::seconds(1));
    }
}

bool g_ready;
void waitReady()
{
	while (!g_ready) {
		this_thread::yield();
	}
	cout << "ok" << endl;
}
void func4() {
    thread t(waitReady);
	t.detach();
    std::this_thread::sleep_for(std::chrono::seconds(2));
    g_ready = true;
    cout << "main thread ends" << endl;
    
}
int main() {
    func4();
    return 0;
}